<template>
    <a-form :model="subject" name="basic" :label-col="{ span: 4 }" :wrapper-col="{ span: 16 }" autocomplete="off"
        @finish="handleClick">
        <a-form-item label="课程名称" name="subjectName" :rules="[{message: '请输入课程' }]">
            <a-input v-model:value="subject.subjectName" />
        </a-form-item>
        <a-form-item label="所在年级" name="gradeID">
            <a-select v-model:value="subject.gradeID" placeholder="please select your zone">
                <a-select-option value="0">请选择</a-select-option>
                <a-select-option v-for="item in grades" :value="item.gradeID">
                    {{ item.gradeName }}
                </a-select-option>
            </a-select>
        </a-form-item>
        <a-form-item label="学时数" name="classHour" :rules="[{message: '请输入学时数' }]">
            <a-input v-model:value="subject.classHour" />
        </a-form-item>
        <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
            <a-button type="primary" html-type="submit">Submit</a-button>
        </a-form-item>
    </a-form>
</template>
<script>
import axios from 'axios';
export default {
    data() {
        return {
            grades: [],
            subject: {
                subjectName: '',
                gradeID: '0',
                classHour: ''
            }
        }
    },
    methods: {
        loadgrades() {
            axios.get('http://localhost:8080/grade/all',).then(res => {
                if (res.data.success) {
                    this.grades = res.data.data
                }
            })
        },
        handleClick() {
            let data = {
                subjectName: this.subject.subjectName,
                gradeID: this.subject.gradeID,
                classHour: this.subject.classHour
            }
            axios.post('http://localhost:8080/subject/', data)
                .then(res => {
                    if (res.data.success) {
                        alert('添加课程成功')
                        this.subject = {
                            subjectName: '',
                            gradeID: '0',
                            classHour: ''
                        }
                    }
                })
        }
    },
    created() {
        this.loadgrades()
    }
}
</script>
<style scoped></style>